package com.m3958.firstgwt.server.model;

import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import com.m3958.firstgwt.server.types.JsonExcludeFields;
import com.m3958.firstgwt.server.utils.JsonlibDateValueProssor;

@Entity
@Table(name="KEY_VALUE",uniqueConstraints={@UniqueConstraint(columnNames={"name","mykey"})})
@AttributeOverride(name="id",column=@Column(nullable = false,name="KV_ID"))
@NamedQueries({
	@NamedQuery(name="findKeyValuesByName",
			query="SELECT k FROM KeyValue AS k WHERE k.name = :name"),
	@NamedQuery(name="findValueByNameAndKey",
		query="SELECT k.myvalue FROM KeyValue AS k WHERE k.name = :name AND k.mykey = :mykey")

})

public class KeyValue extends BaseModel<KeyValue>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static class NamedQueries{
		public final static String FIND_KEY_VALUES_BY_NAME = "findKeyValuesByName";
		public final static String FIND_VALUE_BY_NAME_AND_KEY = "findValueByNameAndKey";
	}

	
	@Override
	public JSONObject toJson(){
		JsonConfig jc = new JsonConfig();
		jc.registerJsonValueProcessor(Date.class, injector.getInstance(JsonlibDateValueProssor.class));
		jc.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		jc.setExcludes(JsonExcludeFields.TAB_EXCLUDES);
		return JSONObject.fromObject(this, jc);
	}	
	
	private String name;
	
	private String mykey;
	
	private String myvalue;
	
	

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	@Override
	public String[] getJsonExcludes() {
		return null;
	}

	public String getMykey() {
		return mykey;
	}

	public void setMykey(String mykey) {
		this.mykey = mykey;
	}

	public String getMyvalue() {
		return myvalue;
	}

	public void setMyvalue(String myvalue) {
		this.myvalue = myvalue;
	}
}
